/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.persistance;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import nominalitzacions.domain.NominalizatorDictionary;

/**
 *
 * @author marc.mauri
 */
public class DictionaryDataManager {

    private File dicoFile;

    DictionaryDataManager(String path) {
        dicoFile = new File(path);
    }

    NominalizatorDictionary loadDictionary() throws FileNotFoundException {
        Scanner dicoScanner = new Scanner(dicoFile);
        NominalizatorDictionary ed = new NominalizatorDictionary();

        if (dicoScanner.hasNextLine() ) ed.setLanguageName(dicoScanner.nextLine() );
        if (dicoScanner.hasNextLine() ) ed.setType(dicoScanner.nextLine() );

        while (dicoScanner.hasNextLine() ) {
            ed.addWord(dicoScanner.nextLine() );
        }

        dicoScanner.close();

        return ed;
    }

     void storeDictionary(NominalizatorDictionary d) throws IOException {
        FileWriter dicoWriter = new FileWriter(dicoFile);
        BufferedWriter dicoBuffer = new BufferedWriter(dicoWriter);

        dicoBuffer.write(d.getLanguageName() );
        dicoBuffer.newLine();
        dicoBuffer.write(d.getType() );
        dicoBuffer.newLine();
        ArrayList<String> dico = d.getAllDiccionary();
        Iterator<String> iterator = dico.iterator();
        while (iterator.hasNext() ) {
            dicoBuffer.write(iterator.next() );
            dicoBuffer.newLine();
        }
        dicoBuffer.close();
    }

    boolean newDictionary() throws IOException {
        return dicoFile.createNewFile();
    }

    boolean deleteDictionary() throws IOException {
        return dicoFile.delete();
    }
}

